package com.colodoo.code.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * 数据表-实体类
 *
 * @author zhisan
 */
@Entity
@Table(name = "code_table_inf")
@Data
@EqualsAndHashCode(exclude = "modules")
public class TableInf implements Serializable {

	/**
	 * 数据表ID
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "table_id")
	private Integer tableId;

	/**
	 * 数据表名
	 */
	@Column(name = "table_name")
	private String tableName;

	/**
	 * 数据表注释
	 */
	@Column(name = "table_comment")
	private String tableComment;

	/**
	 * 模块
	 */
	@ManyToMany(mappedBy = "tables")
	@JsonIgnore
	private Set<ModuleInf> modules = new HashSet<ModuleInf>();

	/**
	 * 表字段
	 */
	@ManyToMany(targetEntity = ColumnInf.class, cascade = {CascadeType.ALL})
	@JoinTable(name = "code_table_column_rel",
			joinColumns = {@JoinColumn(name = "table_id", referencedColumnName = "table_id")},
			inverseJoinColumns = {@JoinColumn(name = "column_id", referencedColumnName = "column_id")}
	)
	private Set<ColumnInf> columns = new HashSet<ColumnInf>();

}
